/**
 * 模板管理API服务
 */
import { request } from "@/http/axios"

export interface Template {
  id: number
  name: string
  displayName: string
  description?: string
  framework: string
  language: string
  version: string
  category: "BACKEND" | "FRONTEND" | "FULLSTACK"
  tags: string[]
  logo?: string
  author: string
  giteeUrl?: string
  githubUrl?: string
  downloadUrl?: string
  downloadCount: number
  rating: number
  status: "ACTIVE" | "INACTIVE"
  features: string[]
  dependencies: Record<string, string>
  templatePath: string
  configSchema?: any
  createdAt: string
  updatedAt: string
}

export interface TemplateRequest {
  name: string
  displayName: string
  description?: string
  framework: string
  language: string
  version: string
  category: "BACKEND" | "FRONTEND" | "FULLSTACK"
  tags: string[]
  logo?: string
  author: string
  giteeUrl?: string
  githubUrl?: string
  downloadUrl?: string
  features: string[]
  dependencies: Record<string, string>
  templatePath: string
  configSchema?: any
  status?: "ACTIVE" | "INACTIVE"
}

export type TemplateListResponse = ApiResponseData<{
  records: Template[]
  total: number
  current: number
  size: number
}>

export type TemplateResponse = ApiResponseData<Template>

export class TemplateApiService {
  /**
   * 获取模板列表
   */
  static async getTemplates(params?: {
    current?: number
    size?: number
    category?: string
    framework?: string
    language?: string
    status?: string
    keyword?: string
  }): Promise<TemplateListResponse> {
    return request<TemplateListResponse>({
      url: "template/list",
      method: "get",
      params
    })
  }

  /**
   * 获取模板详情
   */
  static async getTemplate(id: number): Promise<TemplateResponse> {
    return request<TemplateResponse>({
      url: `template/${id}`,
      method: "get"
    })
  }

  /**
   * 创建模板
   */
  static async createTemplate(data: TemplateRequest): Promise<TemplateResponse> {
    return request<TemplateResponse>({
      url: "template",
      method: "post",
      data
    })
  }

  /**
   * 更新模板
   */
  static async updateTemplate(id: number, data: Partial<TemplateRequest>): Promise<TemplateResponse> {
    return request<TemplateResponse>({
      url: `template/${id}`,
      method: "put",
      data
    })
  }

  /**
   * 删除模板
   */
  static async deleteTemplate(id: number): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `template/${id}`,
      method: "delete"
    })
  }

  /**
   * 批量删除模板
   */
  static async batchDeleteTemplates(ids: number[]): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: "template/batch",
      method: "delete",
      data: { ids }
    })
  }

  /**
   * 启用/禁用模板
   */
  static async toggleTemplate(id: number, status: "ACTIVE" | "INACTIVE"): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `template/${id}/toggle`,
      method: "post",
      data: { status }
    })
  }

  /**
   * 获取模板分类统计
   */
  static async getTemplateStats(): Promise<ApiResponseData<{
    totalCount: number
    categoryStats: { category: string, count: number }[]
    frameworkStats: { framework: string, count: number }[]
    languageStats: { language: string, count: number }[]
  }>> {
    return request<ApiResponseData<{
      totalCount: number
      categoryStats: { category: string, count: number }[]
      frameworkStats: { framework: string, count: number }[]
      languageStats: { language: string, count: number }[]
    }>>({
      url: "template/stats",
      method: "get"
    })
  }

  /**
   * 下载模板
   */
  static async downloadTemplate(id: number): Promise<Blob> {
    return request<Blob>({
      url: `template/${id}/download`,
      method: "get",
      responseType: "blob"
    })
  }

  /**
   * 增加下载次数
   */
  static async incrementDownloadCount(id: number): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `template/${id}/download/increment`,
      method: "post"
    })
  }

  /**
   * 评价模板
   */
  static async rateTemplate(id: number, rating: number): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `template/${id}/rate`,
      method: "post",
      data: { rating }
    })
  }

  /**
   * 获取推荐模板
   */
  static async getRecommendedTemplates(limit?: number): Promise<ApiResponseData<Template[]>> {
    return request<ApiResponseData<Template[]>>({
      url: "template/recommended",
      method: "get",
      params: { limit }
    })
  }
}
